package com.huawei.appgallery.common.media.activity;

import android.app.ActionBar;
import android.content.Context;
import android.os.Build.VERSION;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager.widget.ViewPager.OnPageChangeListener;
import com.huawei.appgallery.common.media.api.IImageBrowseProtocol;
import com.huawei.appgallery.common.media.api.IImageBrowseResult;
import com.huawei.appgallery.common.media.api.OriginalMediaBean;
import com.huawei.appgallery.common.media.bean.SelectedMediaInfo;
import com.huawei.appgallery.foundation.ui.framework.activity.AbstractBaseActivity;
import com.huawei.hmf.annotation.ActivityDefine;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import o.blb;
import o.ble.g;
import o.ble.i;
import o.ble.j;
import o.blf;
import o.blj;
import o.blm;
import o.bln;
import o.cdo;
import o.cdu;
import o.cgv;
import o.cgy;
import o.ckv;
import o.dmi;
import o.dmp;
import o.dmq;
import o.ContentManager;
import o.fiv;
import o.fjf;

@ActivityDefine(alias = "ImageBrowse", protocol = IImageBrowseProtocol.class, result = IImageBrowseResult.class)
public class ImageBrowserActivity extends AbstractBaseActivity {
    private View ʻॱ;
    private CheckBox ʽॱ;
    private LinearLayout ʾ;
    private long ʿ = Long.MAX_VALUE;
    private int ˈ;
    private ImageView ˊˊ;
    private String ˊॱ;
    private fiv ˊᐝ = fiv.ॱ(this);
    private Handler ˋˊ = new Handler();
    private int ˋॱ = 1;
    private String[] ˏॱ;
    private HashMap<Integer, SelectedMediaInfo> ͺ = new HashMap();
    private List<OriginalMediaBean> ॱˊ = new ArrayList();
    private String[] ॱˋ;
    private TextView ॱˎ;
    private int ᐝॱ = 0;

    class c implements OnTouchListener {
        final /* synthetic */ ImageBrowserActivity ॱ;

        private c(ImageBrowserActivity imageBrowserActivity) {
            this.ॱ = imageBrowserActivity;
        }

        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (this.ॱ.ˋॱ <= this.ॱ.ॱˊ.size()) {
                if (this.ॱ.ͺ.containsKey(Integer.valueOf(this.ॱ.ˏ(this.ॱ.ˋॱ)))) {
                    return false;
                }
                if (this.ॱ.ˋॱ >= 1) {
                    OriginalMediaBean originalMediaBean = (OriginalMediaBean) this.ॱ.ॱˊ.get(this.ॱ.ˋॱ - 1);
                    if (originalMediaBean != null && ((long) originalMediaBean.ʻ()) > this.ॱ.ʿ) {
                        this.ॱ.ˋ(view.getContext());
                        return true;
                    }
                }
            }
            return false;
        }
    }

    static class e implements dmi {
        private String ˊ;
        private String[] ˋ;
        private WeakReference<ImageBrowserActivity> ˎ;
        private String[] ॱ;

        public e(ImageBrowserActivity imageBrowserActivity, String str, String[] strArr, String[] strArr2) {
            this.ˎ = new WeakReference(imageBrowserActivity);
            this.ˊ = str;
            this.ˋ = strArr2;
            this.ॱ = strArr;
        }

        public void run() {
            List ˏ = bln.ˊ().ˏ(ContentManager.getInstant().getContext(), this.ˊ, "image", this.ˋ, this.ॱ);
            ImageBrowserActivity imageBrowserActivity = (ImageBrowserActivity) this.ˎ.get();
            if (imageBrowserActivity != null) {
                imageBrowserActivity.ॱˊ = ˏ;
                imageBrowserActivity.ˌ();
            }
        }
    }

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(j.ॱ);
        ᐝ();
        ॱˊ();
    }

    private void ᐝ() {
        this.ʻॱ = findViewById(b.ͺॱ);
        ActionBar actionBar = getActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        if (VERSION.SDK_INT >= 21) {
            Window window = getWindow();
            cgv.ˋ(window);
            cdu.ˊ(this, 16908290, null, false);
            if (cgv.ˎ()) {
                cgv.ˏ(window, 1);
            } else {
                window.setStatusBarColor(-16777216);
            }
            this.ʻॱ.setLayoutParams(new LayoutParams(-1, cdo.ˋ(this)));
            this.ʻॱ.setVisibility(0);
            return;
        }
        this.ʻॱ.setVisibility(8);
    }

    private void ॱˊ() {
        IImageBrowseProtocol iImageBrowseProtocol = (IImageBrowseProtocol) this.ˊᐝ.ॱ();
        this.ˊॱ = iImageBrowseProtocol.getBrowseGroupName();
        List selectedImages = iImageBrowseProtocol.getSelectedImages();
        if (selectedImages != null && selectedImages.size() > 0) {
            this.ͺ = blm.ॱ(selectedImages);
        }
        this.ᐝॱ = iImageBrowseProtocol.getBrowseStartPostion();
        if (this.ᐝॱ < 0) {
            this.ᐝॱ = 0;
        }
        this.ˈ = iImageBrowseProtocol.getMaxSelectSize();
        long maxSelectFileSize = iImageBrowseProtocol.getMaxSelectFileSize();
        if (maxSelectFileSize > 0) {
            this.ʿ = maxSelectFileSize;
        }
        this.ˏॱ = iImageBrowseProtocol.getMimeTyes();
        this.ॱˋ = iImageBrowseProtocol.getCheckFileExtendNames();
        this.ॱˊ = iImageBrowseProtocol.getBrowseImages();
        if ((this.ॱˊ == null || this.ॱˊ.size() == 0) && !cgy.ˎ(this.ˊॱ)) {
            dmq.ˏ.ˊ(dmp.CONCURRENT, new e(this, this.ˊॱ, this.ˏॱ, this.ॱˋ));
        } else {
            ͺ();
        }
    }

    private void ͺ() {
        ˊᐝ();
        this.ˋॱ = this.ᐝॱ + 1;
        if (this.ॱˊ != null && this.ॱˊ.size() != 0) {
            this.ʾ = (LinearLayout) findViewById(b.ʽॱ);
            this.ʽॱ = (CheckBox) findViewById(b.ˊˋ);
            this.ˊˊ = (ImageView) findViewById(b.ˋᐝ);
            ˊˊ();
            if (this.ˈ == 1) {
                this.ʽॱ.setVisibility(8);
                this.ˊˊ.setVisibility(0);
                ʼॱ();
                return;
            }
            this.ˊˊ.setVisibility(8);
            this.ʽॱ.setVisibility(0);
            ˋᐝ();
            ˉ();
            ᐝॱ();
            ˋˊ();
        }
    }

    private void ᐝॱ() {
        this.ʾ.setOnClickListener(new OnClickListener(this) {
            final /* synthetic */ ImageBrowserActivity ˏ;

            {
                this.ˏ = r1;
            }

            public void onClick(View view) {
                this.ˏ.ʽॱ.setChecked(!this.ˏ.ʽॱ.isChecked());
            }
        });
    }

    private void ʼॱ() {
        if (this.ˊˊ.getVisibility() == 0) {
            this.ʾ.setOnClickListener(new OnClickListener(this) {
                final /* synthetic */ ImageBrowserActivity ˋ;

                {
                    this.ˋ = r1;
                }

                public void onClick(View view) {
                    if (this.ˋ.ˋॱ <= this.ˋ.ॱˊ.size() && this.ˋ.ˋॱ >= 1) {
                        OriginalMediaBean originalMediaBean = (OriginalMediaBean) this.ˋ.ॱˊ.get(this.ˋ.ˋॱ - 1);
                        if (originalMediaBean == null || ((long) originalMediaBean.ʻ()) <= this.ˋ.ʿ) {
                            this.ˋ.ˏ(this.ˋ.ˏ(this.ˋ.ˋॱ), this.ˋ.ˋॱ);
                            this.ˋ.ˋˋ();
                            return;
                        }
                        this.ˋ.ˋ(view.getContext());
                    }
                }
            });
        }
    }

    private void ˋ(Context context) {
        int i = (int) (this.ʿ / 1048576);
        ckv.ˎ(context.getResources().getQuantityString(i.ˋ, i, new Object[]{Integer.valueOf(i)}), 0);
    }

    private void ˉ() {
        this.ʾ.setOnTouchListener(new c());
        this.ʽॱ.setOnTouchListener(new c());
    }

    private void ˋˊ() {
        this.ʽॱ.setOnCheckedChangeListener(new OnCheckedChangeListener(this) {
            final /* synthetic */ ImageBrowserActivity ॱ;

            {
                this.ॱ = r1;
            }

            public void onCheckedChanged(CompoundButton compoundButton, boolean z) {
                int ˎ = this.ॱ.ˏ(this.ॱ.ˋॱ);
                if (z) {
                    this.ॱ.ˏ(ˎ, this.ॱ.ˋॱ);
                } else {
                    SelectedMediaInfo selectedMediaInfo = (SelectedMediaInfo) this.ॱ.ͺ.remove(Integer.valueOf(ˎ));
                    if (selectedMediaInfo != null) {
                        int i = selectedMediaInfo.ˏ;
                        if (i <= this.ॱ.ͺ.size()) {
                            for (Integer num : this.ॱ.ͺ.keySet()) {
                                selectedMediaInfo = (SelectedMediaInfo) this.ॱ.ͺ.get(num);
                                if (selectedMediaInfo.ˏ > i) {
                                    selectedMediaInfo.ˏ--;
                                }
                            }
                        }
                    } else {
                        return;
                    }
                }
                this.ॱ.ˊˋ();
                this.ॱ.ʽॱ.setChecked(z);
            }
        });
    }

    private void ˊˊ() {
        ViewPager viewPager = (ViewPager) findViewById(b.ʿ);
        viewPager.setAdapter(new blb(this.ॱˊ));
        viewPager.setCurrentItem(this.ᐝॱ);
        viewPager.setOffscreenPageLimit(2);
        viewPager.setOnPageChangeListener(new OnPageChangeListener(this) {
            final /* synthetic */ ImageBrowserActivity ˏ;

            {
                this.ˏ = r1;
            }

            public void onPageScrollStateChanged(int i) {
                if (i < this.ˏ.ॱˊ.size()) {
                    this.ˏ.ˋॱ = i + 1;
                    if (this.ˏ.ͺ.containsKey(Integer.valueOf(this.ˏ.ˏ(this.ˏ.ˋॱ)))) {
                        this.ˏ.ʽॱ.setChecked(true);
                        this.ˏ.ʽॱ.setEnabled(true);
                        this.ˏ.ʾ.setEnabled(true);
                        return;
                    }
                    this.ˏ.ʽॱ.setChecked(false);
                    if (this.ˏ.ͺ.size() >= this.ˏ.ˈ) {
                        this.ˏ.ʽॱ.setEnabled(false);
                        this.ˏ.ʾ.setEnabled(false);
                    }
                }
            }

            public void onPageScrolled(int i, float f, int i2) {
            }

            public void onPageScrolled(int i) {
            }
        });
    }

    private void ˊᐝ() {
        View findViewById = findViewById(b.ˊᐝ);
        findViewById(b.ᐝॱ).setOnClickListener(new OnClickListener(this) {
            final /* synthetic */ ImageBrowserActivity ˋ;

            {
                this.ˋ = r1;
            }

            public void onClick(View view) {
                this.ˋ.ˋˋ();
            }
        });
        this.ॱˎ = (TextView) findViewById.findViewById(b.ˑ);
        ˊˋ();
    }

    private void ˊˋ() {
        if (this.ˈ > 1) {
            int size = (this.ͺ == null || this.ͺ.size() <= 0) ? 0 : this.ͺ.size();
            this.ॱˎ.setText(getResources().getQuantityString(i.ˏ, this.ˈ, new Object[]{Integer.valueOf(size), Integer.valueOf(this.ˈ)}));
            return;
        }
        this.ॱˎ.setText(getResources().getText(g.ˊ));
    }

    private int ˏ(int i) {
        int i2 = i - 1;
        if (i2 < 0 || i2 >= this.ॱˊ.size()) {
            return 0;
        }
        return ((OriginalMediaBean) this.ॱˊ.get(i2)).ᐝ();
    }

    private void ˋˋ() {
        fjf ˊ = fjf.ˊ(this);
        ((IImageBrowseResult) ˊ.ॱ()).setSelectedMedias(blm.ˎ(this.ͺ));
        setResult(-1, ˊ.ˊ());
        finish();
    }

    private void ˏ(int i, int i2) {
        if (!this.ͺ.containsKey(Integer.valueOf(i))) {
            int size = this.ͺ.size();
            SelectedMediaInfo selectedMediaInfo = new SelectedMediaInfo();
            selectedMediaInfo.ˏ = size + 1;
            selectedMediaInfo.ॱ = (OriginalMediaBean) this.ॱˊ.get(i2 - 1);
            this.ͺ.put(Integer.valueOf(i), selectedMediaInfo);
        }
    }

    private void ˋᐝ() {
        if (this.ᐝॱ < this.ॱˊ.size() && this.ͺ != null) {
            this.ʽॱ.setChecked(this.ͺ.containsKey(Integer.valueOf(((OriginalMediaBean) this.ॱˊ.get(this.ᐝॱ)).ᐝ())));
        }
    }

    public boolean onKeyDown(int i, KeyEvent keyEvent) {
        if (i != 4) {
            return super.onKeyDown(i, keyEvent);
        }
        ˋˋ();
        return true;
    }

    public void onDestroy() {
        super.onDestroy();
        if (cgy.ˎ(this.ˊॱ)) {
            bln.ˊ().ʽ();
            blj.ˋ().ˏ();
        }
    }

    private void ˌ() {
        this.ˋˊ.post(new Runnable(this) {
            final /* synthetic */ ImageBrowserActivity ॱ;

            {
                this.ॱ = r1;
            }

            public void run() {
                if (this.ॱ.ॱˊ == null || this.ॱ.ॱˊ.size() == 0) {
                    blf.ˋ.ˏ("ImageBrowserActivity", "no all images ,error");
                    this.ॱ.finish();
                    return;
                }
                this.ॱ.ͺ();
            }
        });
    }
}
